package com.carkeeper.car.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.car.Itcast;
import com.carkeeper.car.service.ItcastService;
import com.carkeeper.common.constant.StatusCode;
import com.carkeeper.common.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/itcast")
public class ItcastController {

    @Autowired
    private ItcastService itcastService;

    /**
     * 根据主键查询
     * @param id 主键
     * @return 实体数据
     */
    @GetMapping("/{id}")
    public Result<Itcast> findById(@PathVariable Long id) {
        try {
            Itcast itcast = itcastService.findById(id);
            return new Result<>("操作成功！", itcast);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<>(false, StatusCode.ERROR, "操作失败！");
    }

    /**
     * 新增
     * @param itcast 数据
     * @return 操作结果
     */
    @PostMapping
    public Result<String> insert(@RequestBody Itcast itcast) {
        Integer integer = itcastService.insert(itcast);
        if (integer > 0) {
            return new Result<>("操作成功！");
        }
        return new Result<>(false, StatusCode.ERROR, "操作失败！");
    }

    /**
     * 更新
     * @param itcast 数据
     * @return 操作结果
     */
    @PutMapping
    public Result<String> updateById(@RequestBody Itcast itcast) {
        Integer integer = itcastService.updateById(itcast);
        if (integer > 0) {
            return new Result<>("操作成功！");
        }
        return new Result<>(false, StatusCode.ERROR, "操作失败！");
    }

    /**
     * 根据主键删除
     * @param id 主键
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteById(@PathVariable Long id) {
        Integer integer = itcastService.delete(id);
        if (integer > 0) {
            return new Result<>("操作成功！");
        }
        return new Result<>(false, StatusCode.ERROR, "操作失败！");
    }

    /**
     * 根据分页数据查询
     * @param itcast 查询条件
     * @param pageNo 页号
     * @param pageSize 页大小
     * @return 操作结果
     */
    @PostMapping("/page")
    public Result<Page<Itcast>> findPage(@RequestBody Itcast itcast,
                                   @RequestParam(value = "pageNo", defaultValue = "1")Long pageNo,
                                   @RequestParam(value = "pageSize", defaultValue = "10")Long pageSize) {
        try {
            Page<Itcast> page = itcastService.findPage(itcast, pageNo, pageSize);
            return new Result<>("操作成功！", page);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result<>(false, StatusCode.ERROR, "操作失败！");
    }

}
